<style lang="less">
	@import "./main.less";
</style>
<template>
	<div class="main" :class="{'main-hide-text': shrink}">
		<div class="sidebar-menu-con" :style="{width: shrink?'60px':'220px', overflow: shrink ? 'visible' : 'auto'}">
			<shrinkable-menu :shrink="shrink" @on-change="handleSubmenuChange" :theme="menuTheme"
				:before-push="beforePush" :open-names="openedSubmenuArr" :menu-list="menuList">
				<div slot="top" class="logo-con">
					<img v-show="!shrink" src="../images/logo.jpg" key="max-logo" />
					<img v-show="shrink" src="../images/logo-min.jpg" key="min-logo" />
				</div>
			</shrinkable-menu>
		</div>
		<div class="main-header-con" :style="{paddingLeft: shrink?'60px':'220px'}">
			<div class="main-header">
				<div class="navicon-con">
					<Button :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}" type="text"
						@click="toggleClick">
						<Icon type="md-menu" size="35"></Icon>
					</Button>
				</div>
				<div class="header-middle-con">
					<div class="main-breadcrumb">
						<breadcrumb-nav :currentPath="currentPath"></breadcrumb-nav>
					</div>
				</div>
				<div class="header-avator-con">
					<full-screen v-model="isFullScreen" @on-change="fullscreenChange"></full-screen>
					<lock-screen></lock-screen>
					<theme-switch></theme-switch>

					<div class="user-dropdown-menu-con">
						<Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
							<Dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
								<a href="javascript:void(0)">
									<span class="main-user-name">{{ userName }}</span>
									<Icon type="ios-arrow-down"></Icon>
								</a>
								<DropdownMenu slot="list">
									<DropdownItem name="ownSpace">个人中心</DropdownItem>
									<DropdownItem name="loginout" divided>退出登录</DropdownItem>
								</DropdownMenu>
							</Dropdown>
							<Avatar :src="headImgPath" style="background: #619fe7;margin-left: 10px;"></Avatar>
						</Row>
					</div>
				</div>
			</div>
			<div class="tags-con">
				<tags-page-opened :pageTagsList="pageTagsList"></tags-page-opened>
			</div>
		</div>
		<div class="single-page-con" :style="{left: shrink?'60px':'220px'}">
			<div class="single-page">
				<keep-alive :include="cachePage">
					<router-view></router-view>
				</keep-alive>
			</div>
		</div>
	</div>
</template>

<script>
	import shrinkableMenu from "./main_components/shrinkable-menu/shrinkable-menu.vue";
	import tagsPageOpened from "./main_components/tags-page-opened.vue";
	import breadcrumbNav from "./main_components/breadcrumb-nav.vue";
	import fullScreen from "./main_components/fullscreen.vue";
	import lockScreen from "./main_components/lock_screen/lockscreen.vue";
	import themeSwitch from "./main_components/theme_switch/theme_switch.vue";
	import util from "@/libs/util.js";
	import axios from "axios";

	export default {
		components: {
			shrinkableMenu,
			tagsPageOpened,
			breadcrumbNav,
			fullScreen,
			lockScreen,
			themeSwitch
		},
		data() {
			return {
				shrink: false,
				userName: "",
				isFullScreen: false,
				openedSubmenuArr: this.$store.state.app.openedSubmenuArr
			};
		},
		computed: {
			menuList() {
				return this.$store.state.app.menuList;
			},
			pageTagsList() {
				return this.$store.state.app.pageOpenedList; // 打开的页面的页面对象
			},
			currentPath() {
				return this.$store.state.app.currentPath; // 当前面包屑数组
			},
			headImgPath() {
				return sessionStorage.headImg ?
					sessionStorage.headImg :
					require("../images/defaultImg.jpg");
			},
			cachePage() {
				return this.$store.state.app.cachePage;
			},
			lang() {
				return this.$store.state.app.lang;
			},
			menuTheme() {
				return this.$store.state.app.menuTheme;
			}
		},
		methods: {
			init() {
				let pathArr = util.setCurrentPath(this, this.$route.name);
				this.$store.commit("updateMenuList");
				if (pathArr.length >= 2) {
					this.$store.commit("addOpenSubmenu", pathArr[1].name);
				}

				this.userName = JSON.parse(sessionStorage.getItem("userInfo")).nickname;
				this.checkTag(this.$route.name);
			},
			toggleClick() {
				this.shrink = !this.shrink;
			},
			handleClickUserDropdown(name) {
				if (name === "ownSpace") {
					util.openNewPage(this, "own_index");
					this.$router.push({
						name: "own_index"
					});
				} else if (name === "loginout") {
					// 退出登录
					let vm = this;
					axios.get("Login/logout").then(function(response) {
						let res = response.data;
						if (res.code === 1) {
							vm.$store.commit("logout", vm);
							vm.$store.commit("clearOpenedSubmenu");
							vm.$router.push({
								name: "login"
							});
						} else {
							vm.$Message.error(res.msg);
						}
					});
				}
			},
			checkTag(name) {
				let openpageHasTag = this.pageTagsList.some(item => {
					if (item.name === name) {
						return true;
					}
				});
				if (!openpageHasTag) {
					//  解决关闭当前标签后再点击回退按钮会退到当前页时没有标签的问题
					util.openNewPage(
						this,
						name,
						this.$route.params || {},
						this.$route.query || {}
					);
				}
			},
			handleSubmenuChange(val) {},
			beforePush(name) {
				// if (name === "accesstest_index") {
				//   return false;
				// } else {
				//   return true;
				// }
				return true;
			},
			fullscreenChange(isFullScreen) {}
		},
		watch: {
			$route(to) {
				this.$store.commit("setCurrentPageName", to.name);
				let pathArr = util.setCurrentPath(this, to.name);
				if (pathArr.length > 2) {
					this.$store.commit("addOpenSubmenu", pathArr[1].name);
				}
				this.checkTag(to.name);
				localStorage.currentPageName = to.name;
			},
			lang() {
				util.setCurrentPath(this, this.$route.name); // 在切换语言时用于刷新面包屑
			}
		},
		mounted() {
			this.init();
		},
		created() {
			// 显示打开的页面的列表
			this.$store.commit("setOpenedList");
		}
	};
</script>
